<?php
namespace Home\Controller;
class DeviceApiController extends CommonController{
	
	/* 刷新部落奖杯 */
	public function refTrophy(){
		$beginTime = $_POST["beginTime"];
		if(!$beginTime){
			$this->ajaxReturn(0);
		}
		$tribeList = array();
		$userList = M("User")->field("us_id,us_tribe_id,us_tribe_status")->select();
		foreach ($userList as $key => $value) {
			$condition = array(
				'aw_type'=>array('in',array(1,2)),
				'aw_target'=>$value['us_id'],
				'aw_create_time'=>array(array('egt',$beginTime),array('elt',time()))
			);
			$userAwSum = M("AwardLog")->where($condition)->sum('aw_value');
			$userAwSum = $userAwSum?$userAwSum:0;
			M("User")->where(array('us_id'=>$value['us_id']))->save(array('us_trophy_num'=>$userAwSum));
			if($value['us_tribe_id']&&$value['us_tribe_status']==2){
				if(empty($tribeList[$value['us_tribe_id']])){
					$tribeList[$value['us_tribe_id']] = $userAwSum;
				}else{
					$tribeList[$value['us_tribe_id']] += $userAwSum;
				}
			}
		}
		foreach ($tribeList as $k_g => $v_g) {
			$saveData = array(
				'tr_trophy_num'=>$v_g,
			);
			$tribe_level = M("TribeLevel")->where(array('tl_condition'=>array("elt",$v_g)))->order("tl_level desc")->find();
			if(!empty($tribe_level)){
				$saveData['tr_level'] = $tribe_level['tl_level'];
			}else{
				$saveData['tr_level'] = 0;
			}
			M("Tribe")->where(array('tr_id'=>$k_g))->save($saveData);
		}
		$this->ajaxReturn(1);
	}

	/* 设置设备标记 */
    public function setDevTag(){
        $deviceId = $_POST['deviceId'];
        $devData = M("Device")->where(array('dev_id'=>$deviceId))->save(array('dev_tag'=>0));
        $this->ajaxReturn(1);
    }
	/* 删除当前时间X小时前的订单日志记录 */
	public function delOrderLog(){
		$deviceId = $_POST['deviceId'];
		$config_time = time()-3600*4;
		$where = array(
			'ol_create_time'=>array('LT',$config_time)
		);
		M('orderLog')->where($where)->delete();
		$this->ajaxReturn(1);
	}
	/* 删除当前时间X小时前的订单进度 */
	public function delOrderSchedule(){
		$deviceId = $_POST['deviceId'];
		$config_time = time()-3600*72;
		$where = array(
			'os_create_time'=>array('LT',$config_time)
		);
		M('orderSchedule')->where($where)->delete();
		$this->ajaxReturn(1);
	}

	/* 订单日志记录 */
	public function updateOrderLog(){
		$deviceId = $_POST['deviceId'];
		$condition = array(
			'or_device_id'=>$deviceId,
			'or_status'=>array('eq',1),
		);
		/*根据设备编号查询当前未完成的订单*/
		$order = M('Order')->where($condition)->find();
		if(empty($order)){
			$this->ajaxReturn(0);
		}

		/*添加到进度*/
		$olData =array(
			'ol_msg'=>$_POST['msg'],
			'ol_order_id'=>$order['or_id'],
			'ol_create_time'=>time(),
		);
		/* 记录进度 */
		M('orderLog')->add($olData);

		$this->ajaxReturn(1);
	}

	/* 设备故障排查 */
	public function Troubleshooting(){
		$deviceList = M('Device')->where("dev_status != 0")->select();
		foreach ($deviceList as $key => $value) {
			$time = time();
			if((int)$value['dev_update_time']<$time){
				M('Device')->where(array('dev_id'=>$value['dev_id']))->save(array('dev_status'=>0));
				$order = M('Order')->where(array('or_status'=>1,'or_device_id'=>$value['dev_id']))->find();
				if(!empty($order)){
					M('Order')->where(array('or_status'=>1,'or_device_id'=>$value['dev_id']))->save(array('or_status'=>0,'or_device_id'=>null));
				}
			}
		}
		$this->ajaxReturn(1);
	}
	/*  订单状态
		参数说明：deviceId  设备编号
				  status    状态
	*/
	public function updateProgress(){
		$msgConfig = array(
			'1'=>'已成功连接设备,开始登陆',
			'2'=>'账号或密码不正确,请在下面输入账号密码,点击提交信息',
			'3'=>'登陆需要邮箱或短信验证码,请在下面输入,点击提交信息',
			'4'=>'登陆成功',
		);
		$deviceId = $_POST['deviceId'];
		$status = $_POST['status'];
		$condition = array(
			'or_device_id'=>$deviceId,
			'or_status'=>array('eq',1),

		);
		/*根据设备编号查询当前未完成的订单*/
		$order = M('Order')->where($condition)->find();
		if(empty($order)){
			$this->ajaxReturn(0);
		}

		/*添加到进度*/
		$osData =array(
			'os_msg'=>$msgConfig[$status.''],
			'os_order_id'=>$order['or_id'],
			'os_status'=>$status,
			'os_create_time'=>time(),
		);
		/*判断多次错误*/
		$result = true;
		if($status==2||$status==3){
			if($order['or_game_status']==2||$order['or_game_status']==1){
				M('Order')->where($condition)->save(array('or_password'=>'******','or_game_status'=>3,'or_status'=>2,'or_end_time'=>time(),'or_end_msg'=>'挂机中需要验证码，或者密码不正确'));
				M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_status'=>1));
			}else{
				$count = M('orderSchedule')->where('(os_status = 2 or os_status = 3) and os_order_id='.$order['or_id'])->count();
				if($count>5){
					$result = false;
					M('Order')->where($condition)->save(array('or_password'=>'******','or_game_status'=>3,'or_status'=>2,'or_end_time'=>time(),'or_end_msg'=>'错误次数过多'));
					M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_status'=>1));
				}else{
					$cout = 0;
					if($status==2){
						M('Order')->where($condition)->save(array('or_account'=>'空','or_password'=>'空'));
						$cout = M('orderSchedule')->where('os_status = 2 and os_order_id='.$order['or_id'])->count();
					}else{
						$cout = M('orderSchedule')->where('os_status = 3 and os_order_id='.$order['or_id'])->count();
						M('Order')->where($condition)->save(array('or_code'=>'空'));
					}
					$osData['os_msg'] = $osData['os_msg'].'('.((int)$cout+1).')';
				}
			}
		}
		if($status==4){
			M('Order')->where($condition)->save(array('or_game_status'=>1));
		}

		/* 记录进度 */
		M('orderSchedule')->add($osData);

		$this->ajaxReturn(1);
	}

	/* 设备绑定ID */
	public function bindingOrder(){
		$deviceId = $_POST['deviceId'];
		$versionId = $_POST['versionId'];
		$time = $_POST['time'];
		M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_update_time'=>$time,'dev_version_id'=>$versionId));
		$where = array(
			'or_status'=>array('eq',0),
			'or_version_id'=>$versionId,
		);
		$device = M('Device')->where(array('dev_id'=>$deviceId))->find();
		if(empty($device)){
			$this->ajaxReturn(2);
		}
		$check = M('Order')->field('or_id')->where(array('or_status'=>1,'or_device_id'=>$deviceId))->find();
		if(empty($check)){
			$order = M('Order')->field('or_id')->where($where)->order("or_create_time asc")->find();
			if(!empty($order)){
				M('Order')->where(array('or_id'=>$order['or_id']))->save(array('or_device_id'=>$deviceId,'or_status'=>1));
				M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_status'=>2));
				$this->ajaxReturn(1);
			}else{
				M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_status'=>1));
				$this->ajaxReturn(0);
			}
		}else{
			$this->ajaxReturn(2);
		}
	}

	/* 拉取订单信息 */
	public function getOrderInfo(){
		$deviceId = $_POST['deviceId'];
		$condition = array(
			'or_device_id'=>$deviceId,
			'or_status'=>array('eq',1),
		);
		//根据设备编号查询当前未完成的订单
		$order = M('Order')->field('or_user_id,or_account,or_password,or_code,or_version,or_status,or_game_status,or_param')->where($condition)->find();
		if(empty($order)){
			$this->ajaxReturn(0);
		}
		$user = M('User')->field('us_phone')->where(array('us_id'=>$order['or_user_id']))->find();
		$order['us_phone'] = $user['us_phone'];
		$order['or_param'] = json_decode($order['or_param'],true);
		$this->ajaxReturn($order);
	}

	/* 跟新订单资料数据 */
	public function updateOrderInfo(){
		$deviceId = $_POST['deviceId'];
		$time = $_POST['time'];
		M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_update_time'=>$time));

		$gold_medal = (int)$_POST['gold_medal'];
		$silver_medal = (int)$_POST['silver_medal'];
		$bronze_medal = (int)$_POST['bronze_medal'];

		$condition = array(
			'or_device_id'=>$deviceId,
			'or_status'=>array('eq',1),
		);
		//根据设备编号查询当前未完成的订单
		$order = M('Order')->where($condition)->find();
		if(empty($order)){
			$this->ajaxReturn(0);
		}
		$saveData = array(
			'or_gold_medal'=>(int)$order['or_gold_medal']+$gold_medal,
			'or_silver_medal'=>(int)$order['or_silver_medal']+$silver_medal,
			'or_bronze_medal'=>(int)$order['or_bronze_medal']+$bronze_medal,
		);
		M('Order')->where(array('or_id'=>$order['or_id']))->save($saveData);
		$this->ajaxReturn(1);
	}

	/* 结束订单 */
	public function endingOrder(){
		$deviceId = $_POST['deviceId'];
		$msg = $_POST['msg'];
		$condition = array(
			'or_device_id'=>$deviceId,
			'or_status'=>array('eq',1),
		);
		//根据设备编号查询当前未完成的订单
		$order = M('Order')->field('or_id,or_user_id,or_account,or_password,or_code,or_version,or_status,or_param')->where($condition)->find();
		if(empty($order)){
			$this->ajaxReturn(0);
		}
		M('Device')->where(array('dev_id'=>$deviceId))->save(array('dev_status'=>1));
		$saveData = array(
			'or_status'=>2,
			'or_game_status'=>3,
			'or_end_time'=>time(),
			'or_end_msg'=>$msg,
			'or_password'=>'******',
		);
		M('Order')->where(array('or_id'=>$order['or_id']))->save($saveData);
		$this->ajaxReturn(1);
	}
}
?>